Skip to main content

Crate mq_markdown

Crate mq_markdown 

Source
Expand description

§mq-markdown: Markdown parsing and manipulation for mq

This crate provides comprehensive markdown parsing, manipulation, and conversion functionality used in mq. It offers a robust API to work with markdown content and generate different output formats.

§Features

  • Parse Markdown: Convert markdown strings to structured AST
  • HTML Conversion: Convert between markdown and HTML formats
  • MDX Support: Parse and manipulate MDX (Markdown + JSX) content
  • JSON Export: Serialize markdown AST to JSON (with json feature)
  • Configurable Rendering: Customize output formatting and styles
  • Built-in parser: CommonMark, GFM (tables, strikethrough, autolink literals, task lists, footnotes), frontmatter, math and MDX are parsed, and rendered to HTML, by a parser of this crate.
  • Obsidian syntax: with the wikilink, embed and callout features, [[links]], ![[embeds]] and > [!callouts] are parsed, and rendered to HTML with the classes Obsidian uses (internal-link, callout). Link targets are used as URLs as written.

§Quick Start

§Basic HTML Conversion

use mq_markdown::to_html;

let markdown = "# Hello, world!";
let html = to_html(markdown);
assert_eq!(html, "<h1>Hello, world!</h1>");

§Working with Markdown AST

use mq_markdown::Markdown;

let markdown = "# Heading\n\nParagraph with *emphasis*";
let doc = markdown.parse::<Markdown>().unwrap();

println!("Found {} nodes", doc.nodes.len());
println!("HTML: {}", doc.to_html());
println!("Text: {}", doc.to_text());

§Custom Rendering Options

use mq_markdown::{Markdown, RenderOptions, ListStyle};

let mut doc = "- Item 1\n- Item 2".parse::<Markdown>().unwrap();
doc.set_options(RenderOptions {
    list_style: Some(ListStyle::Plus),
    ..Default::default()
});

// Now renders with "+" instead of "-"
println!("{}", doc);

§Performance Considerations

  • Use &str methods when possible to avoid unnecessary allocations
  • The AST uses structural equality checking for efficient comparisons
  • Consider using CompactString for memory-efficient string storage
  • Position information can be omitted to reduce memory usage

§HTML to Markdown (optional feature)

When the html-to-markdown feature is enabled, this crate can also convert HTML to Markdown.

ⓘ
// This example requires the `html-to-markdown` feature.
// Add `mq-markdown = { version = "...", features = ["html-to-markdown"] }` to your Cargo.toml.
use mq_markdown::convert_html_to_markdown;

let html = "<p>Hello <strong>world</strong>!</p>";
let markdown = convert_html_to_markdown(html)?;
assert_eq!(markdown, "Hello **world**!");

Structs§

AttrSpec
An attribute a node kind has, with its type. optional attributes are none when absent.
Blockquote
A block quote.
Break
A hard line break.
Code
A code block.
CodeInline
Inline code.
ColorTheme
Color theme for rendering markdown nodes with optional ANSI escape codes.
Definition
A link reference definition.
Delete
Strikethrough text.
Emphasis
Emphasized text.
Footnote
A footnote definition.
FootnoteRef
A footnote reference.
Fragment
A sequence of nodes with no wrapper.
Heading
A heading.
HeadingDepth
The depth of a heading, from 1 to 6.
HorizontalRule
A thematic break.
Html
Raw HTML.
Image
An inline image.
ImageRef
A reference-style image.
Link
An inline link.
LinkRef
A reference-style link.
List
A list item.
Markdown
A Markdown document.
Math
A math block.
MathInline
Inline math.
MdxDiagnostic
An MdxError with the document, to show where in it the error is.
MdxError
Invalid MDX, with where it is in the document when that is known.
MdxFlowExpression
A block-level MDX expression.
MdxJsEsm
MDX ESM import or export.
MdxJsxAttribute
A property of a JSX element, with the value it has after its quotes or braces.
MdxJsxFlowElement
A block-level JSX element.
MdxJsxTextElement
An inline JSX element.
MdxTextExpression
An inline MDX expression.
ParseOptions
What reading Markdown or MDX turns on.
Point
A location in the source.
Position
A span of the source.
RenderOptions
Options for writing nodes as Markdown.
Strong
Strongly emphasized text.
TableAlign
The delimiter row of a table, giving the column alignments.
TableCell
A table cell.
TableRow
A table row.
Text
Plain text.
Title
A link title.
Toml
TOML front matter.
Url
A link destination.
Yaml
YAML front matter.

Enums§

AttrType
The type of a node attribute as returned by Node::attr.
AttrValue
Represents a typed attribute value that can be returned from or passed to attr/set_attr methods.
HorizontalRuleMarker
The character a horizontal rule is written with in the source.
ListMarker
The marker of a list item as it is written in the source.
ListStyle
Bullet marker used when writing lists.
MdxAttributeContent
An attribute of a JSX element: a property such as a="b", or an expression such as {...props}.
MdxAttributeValue
The value of a JSX property: a string, or an expression in braces.
MdxErrorKind
What is wrong with invalid MDX.
MdxFound
What a JSX tag has where something else must be.
MdxPlace
The part of a JSX tag where an unexpected character is.
Node
A Markdown node.
NodeKind
The kind of a Node. Headings are split by depth, so every Node has exactly one kind.
SetAttrError
The error of Node::set_attr.
TableAlignKind
The alignment of a table column.
TitleSurroundStyle
How a link title is quoted when written.
UrlSurroundStyle
How a link destination is wrapped when written.

Functions§

to_html
Converts Markdown to HTML.